package com.whyl.eems.common.sms;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.whyl.eems.common.sms.config.SmsConfig;
import org.springframework.stereotype.Component;

import java.util.Random;

/**
 * 作者：阿坤
 * 创建时间：13:45 2019/8/14
 * 功能描述：阿里云短信
 */
@Component
public class ALiSendSMS {

    /**
     * 功能:发送短信
     *
     * @param phoneNumbers  短信接收人的手机号码
     * @param signName      阿里云签名
     * @param templateCode  阿里云短信模板
     * @param templateParam 随机码
     */
    public CommonResponse sendSms(String phoneNumbers, String signName, String templateCode, String templateParam) {
        try {
            DefaultProfile profile = DefaultProfile.getProfile("default", SmsConfig.A_LI_KEY_ID, SmsConfig.A_LI_ACCESS_SECRET);
            IAcsClient client = new DefaultAcsClient(profile);

            CommonRequest request = new CommonRequest();
            //request.setProtocol(ProtocolType.HTTPS);
            request.setMethod(MethodType.POST);
            request.setDomain("dysmsapi.aliyuncs.com");
            request.setVersion("2017-05-25");
            request.setAction("SendSms");
            request.putQueryParameter("PhoneNumbers", phoneNumbers);
            request.putQueryParameter("SignName", signName);
            request.putQueryParameter("TemplateCode", templateCode);
            request.putQueryParameter("TemplateParam", "{\"code\":\"" + templateParam + "\"}");

            return client.getCommonResponse(request);
        } catch (ServerException e) {
            e.printStackTrace();
            return null;
        } catch (ClientException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 批量发送
     *
     * @param phoneNumberJson   手机号数组  [“15900000000”,”13500000000”]
     * @param signNameJson      阿里云签名数据 [“阿里云”,”阿里巴巴”]
     * @param templateCode      阿里云短信模板 SMS_152550005
     * @param templateParamJson 随机码 [{“name”:”TemplateParamJson”},{“name”:”TemplateParamJson”}]
     */
    public void SendBatchSms(String phoneNumberJson, String signNameJson, String templateCode, String templateParamJson) {
        DefaultProfile profile = DefaultProfile.getProfile("default", SmsConfig.A_LI_KEY_ID, SmsConfig.A_LI_ACCESS_SECRET);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        //request.setProtocol(ProtocolType.HTTPS);
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendBatchSms");
        request.putQueryParameter("PhoneNumberJson", phoneNumberJson);
        request.putQueryParameter("SignNameJson", signNameJson);
        request.putQueryParameter("TemplateCode", templateCode);
        request.putQueryParameter("TemplateParamJson", templateParamJson);
        try {
            CommonResponse response = client.getCommonResponse(request);
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }


    /**
     * 功能: 生成随机数
     */
    public String readm(int max) {
        Random random = new Random();
        String result = "";
        if (max < 0) {
            max = 6;
        }
        for (int i = 0; i < max; i++) {
            result += random.nextInt(10);
        }
        return result;
    }
}
